package com.demo.redis.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.demo.hibernate.entity.HUserInformation;
import com.demo.hibernate.service.HLoginRegisterService;
import com.demo.redis.service.RedisTemplateService;

@RestController
@RequestMapping("/redis")
public class RedisController {

	// 注入RedisService服务
	@Autowired
	private RedisTemplateService<String, String> stringRedisTemplateService;
	@Autowired
	private RedisTemplateService<String, HUserInformation> beanRedisTemplateService;

	// 注入hibernateService服务
	@Autowired
	private HLoginRegisterService hLoginRegisterService;

	@Autowired
	private CacheManager cacheManager;

	@RequestMapping("/login")
	public HUserInformation login(HUserInformation userInformation) {
		System.err.println("redis");
		System.err.println("参数：" + userInformation);
		HUserInformation login = hLoginRegisterService.login(userInformation);
		System.err.println("结果:" + login);
		login = hLoginRegisterService.login(userInformation);
		System.err.println("缓存:" + login);
		try {
			
			stringRedisTemplateService.set("p1", "pakho");
			beanRedisTemplateService.set("p2", userInformation);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

		return userInformation;
	}

	@RequestMapping("/register")
	public HUserInformation register(HUserInformation userInformation) {
		System.err.println("redis");
		System.err.println("参数：" + userInformation);
		HUserInformation register = hLoginRegisterService.register(userInformation);
		System.err.println("结果:" + register);
		return userInformation;
	}

	@RequestMapping("/clear")
	public String clear() {
		Collection<String> cacheNames = cacheManager.getCacheNames();
		for (String string : cacheNames) {
			System.err.println(string);
		}
		return "success";
	}

}
